package org.dromara.tearoom.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.tearoom.domain.vo.TerTransactionsVo;
import org.dromara.tearoom.domain.bo.TerTransactionsBo;
import org.dromara.tearoom.service.ITerTransactionsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 钱包充值交易流水
 * 前端访问路由地址为:/tearoom/transactions
 *
 * @author xch
 * @date 2025-05-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/transactions")
public class TerTransactionsController extends BaseController {

    private final ITerTransactionsService terTransactionsService;

    /**
     * 查询钱包充值交易流水列表
     */
    @SaCheckPermission("tearoom:transactions:list")
    @GetMapping("/list")
    public TableDataInfo<TerTransactionsVo> list(TerTransactionsBo bo, PageQuery pageQuery) {
        return terTransactionsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出钱包充值交易流水列表
     */
    @SaCheckPermission("tearoom:transactions:export")
    @Log(title = "钱包充值交易流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TerTransactionsBo bo, HttpServletResponse response) {
        List<TerTransactionsVo> list = terTransactionsService.queryList(bo);
        ExcelUtil.exportExcel(list, "钱包充值交易流水", TerTransactionsVo.class, response);
    }

    /**
     * 获取钱包充值交易流水详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("tearoom:transactions:query")
    @GetMapping("/{id}")
    public R<TerTransactionsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(terTransactionsService.queryById(id));
    }

    /**
     * 新增钱包充值交易流水
     */
    @SaCheckPermission("tearoom:transactions:add")
    @Log(title = "钱包充值交易流水", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TerTransactionsBo bo) {
        return toAjax(terTransactionsService.insertByBo(bo));
    }

    /**
     * 修改钱包充值交易流水
     */
    @SaCheckPermission("tearoom:transactions:edit")
    @Log(title = "钱包充值交易流水", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerTransactionsBo bo) {
        return toAjax(terTransactionsService.updateByBo(bo));
    }

    /**
     * 删除钱包充值交易流水
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tearoom:transactions:remove")
    @Log(title = "钱包充值交易流水", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terTransactionsService.deleteWithValidByIds(List.of(ids), true));
    }
}
